package com.exam.demo.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exam.demo.domain.User;
import com.exam.demo.mapper.UserMapper;
import com.exam.demo.result.R;
import com.exam.demo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserMapper baseMapper;

    @RequestMapping("/add")
    public R<String>  add(@RequestBody User user){
        //需要不重复的增删改查则加这一句
        if(!baseMapper.getUserByName(user).isEmpty()) return R.fail("该用户名已存在");
        return R.judge(baseMapper.insert(user)>0);
    }

    @RequestMapping("/del")
    public R<String>  del(@RequestBody User user){
        return R.judge(baseMapper.deleteById(user.getId())>0);
    }


    @RequestMapping("/getInfo")
    public R<User>  getInfo(@RequestBody User user){
        return R.ok(baseMapper.selectById(user.getId()));
    }


    @RequestMapping("/edit")
    public R<String>  edit(@RequestBody User user){
        return R.judge(baseMapper.updateById(user)>0);
    }


    @RequestMapping("/list")
    public R<List<User>> list(@RequestBody User user){
        QueryWrapper<User> qw = new QueryWrapper<>();
        //如果需要查询条件则在这里加
        qw.like(StringUtils.isNotEmpty(user.getName()),"name",user.getName())
          .eq(StringUtils.isNotEmpty(user.getSex()),"sex",user.getSex());
        return R.ok(baseMapper.selectList(qw));
    }





}
